//
//  YYLMapViewController.m
//  YYLSport
//
//  Created by yangyinglei on 2017/4/21.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

#import "YYLMapViewController.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
#import "YYLPolyLine.h"
#import "YYLSportTrack.h"
#import "UIButton+CZAddition.h"
#import <Masonry.h>

@interface YYLMapViewController ()<MAMapViewDelegate>

@property (nonatomic, assign) BOOL hasSetStartAnno;
@property (nonatomic, assign) BOOL isNOTKeepLocationCenter;

@end

@implementation YYLMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [AMapServices sharedServices].enableHTTPS = YES;
    
    ///初始化地图
    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
    //添加我的位置按钮
    UIButton *backBtn = [UIButton cz_textButton:@"返回" fontSize:12 normalColor:[UIColor blueColor] selectedColor:[UIColor darkGrayColor]];
    
    [self.mapView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.top.offset(50);
    }];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    ///把地图添加至view
    [self.view addSubview:_mapView];
    
    [self setupMapView];
}

- (void)setupMapView{
    //关闭3d旋转
    _mapView.rotateCameraEnabled = NO;
    //显示用户位置
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    //开启后台运行模式
    _mapView.allowsBackgroundLocationUpdates = YES;
    
    //取消自动暂停
    _mapView.pausesLocationUpdatesAutomatically = NO;
    
    _mapView.delegate = self;
    
}

- (void)backBtnAction{
    _isNOTKeepLocationCenter = NO;
    
}

#pragma mark - MAMapViewDelegate
//设置覆盖物样式  设置折线
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    //其中overlay 就是我们遮盖物 
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        
        polylineRenderer.lineWidth    = 3.f;
        //设置折线的颜色
        YYLPolyLine *polyline = (YYLPolyLine *)overlay;
        polylineRenderer.strokeColor  = polyline.strokeColor;
        polylineRenderer.lineJoinType = kMALineJoinRound;
        polylineRenderer.lineCapType  = kMALineCapRound;
        
        return polylineRenderer;
    }
    
    return nil;
}

/**
 单击地图时调用

 @param mapView 地图
 @param coordinate 点击的经纬度
 */
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {

    //构造折线数据对象
    CLLocationCoordinate2D commonPolylineCoords[2];
    commonPolylineCoords[0].latitude = self.mapView.userLocation.location.coordinate.latitude;
    commonPolylineCoords[0].longitude = self.mapView.userLocation.location.coordinate.longitude;
    
    commonPolylineCoords[1].latitude = coordinate.latitude;
    commonPolylineCoords[1].longitude = coordinate.longitude;
    
    
    //构造折线对象
    MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:2];
    
    //在地图上添加折线对象
    [_mapView addOverlay: commonPolyline];


}
//更新用户位置时调用
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    //方向改变
    if (updatingLocation == NO) {
        return;
    }
    
    //添加大头针模型    判断地图上有没有大头针 和 大头针模型存在不存在
    if (_hasSetStartAnno == NO && self.track.startAnno) {
        //添加大头针
        [_mapView addAnnotation:self.track.startAnno];
        _hasSetStartAnno = YES;
    }
//    NSLog(@"%@,%@",userLocation.location.timestamp, [NSDate date]);
    if (!self.isNOTKeepLocationCenter) {
        _mapView.centerCoordinate = userLocation.location.coordinate;
    } else {
        _mapView.centerCoordinate = _mapView.centerCoordinate;
    }
    
    MAPolyline *polyline = [self.track appendPolylineWithDest:userLocation.location];
    
    //在地图上添加折线对象
    [_mapView addOverlay: polyline];

}
//设置大头针视图是调用
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }

        //设置图片
        annotationView.image = [UIImage imageNamed:self.track.sportTypeimgName];
        //设置中心点偏移
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height * 0.5);
        return annotationView;
    }
    return nil;
}

//在此要监听地图的拖拽事件    不知道该用哪个方法
//- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState
//   fromOldState:(MAAnnotationViewDragState)oldState{
//
//    self.isNOTKeepLocationCenter = YES;
//}

@end
